package meconsea.life.cms.web.tags;

import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

import meconsea.life.admin.accessor.IndicateAccessor;
import meconsea.life.admin.entity.Indicate;
import meconsea.life.sys.module.SysModule;
import meconsea.life.util.GuiceUtil;

import org.apache.log4j.Logger;
import org.apache.struts2.components.Component;
import org.apache.struts2.util.MakeIterator;
import org.apache.struts2.views.jsp.IteratorStatus;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.opensymphony.xwork2.util.ValueStack;

public class ArtClsList extends Component {
	private static final Logger log = Logger.getLogger(ArtClsList.class);
	private static final long serialVersionUID = -1827978135193591801L;
	
	private IndicateAccessor indicatorAccessor;
	
	protected Iterator iterator;
	protected IteratorStatus status;
	protected Object oldStatus;
	protected IteratorStatus.StatusState statusState;
	protected String statusAttr;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected PageContext pageContext;
	public ArtClsList(ValueStack stack, HttpServletRequest req,
			HttpServletResponse res, PageContext pc) {
		super(stack);
		this.request = req;
		this.response = res;
		this.pageContext = pc;
	}
	//业务属性 clsCode以,为分隔符
	private String clsList;
	public String getClsList() {
		return clsList;
	}

	public void setClsList(String clsList) {
		this.clsList = clsList;
	}
	//业务属性结束
	//标签体开始
	public boolean start(Writer writer) {
		// Create an iterator status if the status attribute was set.
		if (statusAttr != null) {
			statusState = new IteratorStatus.StatusState();
			status = new IteratorStatus(statusState);
		}

		ValueStack stack = getStack();
		//业务属性及业务逻辑开始
		if (clsList.startsWith("%{") && clsList.endsWith("}")) {
			clsList = clsList.substring(2, clsList.length() - 1);
			clsList = (String) this.getStack().findValue(clsList);
		}
		List<String> clsCodeList = new ArrayList<String>();
		if(clsList.indexOf(",") != -1){//以,分隔
			String[] clsCodeArray = clsList.split(",");
			for(String _clsC : clsCodeArray){
				clsCodeList.add(_clsC);
			}
		}else if(clsList.length() < 1){//不存在
			return false;
		}else{//单个clscode
			clsCodeList.add(clsList);
		}
		//生成Iterator
		try{ 
			indicatorAccessor = (IndicateAccessor)GuiceUtil.getInstance().getInstanceClass(IndicateAccessor.class);
			List<Indicate>  ins = indicatorAccessor.getIndicateByClsCodes(clsCodeList);
			iterator = MakeIterator.convert(ins);
		}catch(Exception e){
			log.error("生成"+clsList+"的分类列表出现异常.",e);
			return false;
		}
		//摘自struts2's iterator
		// get the first
		if ((iterator != null) && iterator.hasNext()) {
			Object currentValue = iterator.next();
			stack.push(currentValue);
			String id = getId();
			if ((id != null) && (currentValue != null)) {
				stack.getContext().put(id, currentValue);
			}
			// Status object
			if (statusAttr != null) {
				statusState.setLast(!iterator.hasNext());
				oldStatus = stack.getContext().get(statusAttr);
				stack.getContext().put(statusAttr, status);
			}
			return true;
		} else {
			super.end(writer, "");
			return false;
		}
	}
	
	//标签体结束
	public boolean end(Writer writer, String body) {
		ValueStack stack = getStack();
		if (iterator != null) {
			stack.pop();
		}

		if (iterator != null && iterator.hasNext()) {
			Object currentValue = iterator.next();
			stack.push(currentValue);

			String id = getId();

			if ((id != null) && (currentValue != null)) {
				stack.getContext().put(id, currentValue);
			}

			// Update status
			if (status != null) {
				statusState.next(); // Increase counter
				statusState.setLast(!iterator.hasNext());
			}

			return true;
		} else {
			if (status != null) {
				if (oldStatus == null) {
					stack.getContext().put(statusAttr, null);
				} else {
					stack.getContext().put(statusAttr, oldStatus);
				}
			}
			super.end(writer, "");
			return false;
		}
	}

	/**
	 * if specified, an instanceof IteratorStatus will be pushed into stack upon
	 * each iteration
	 * 
	 * @s.tagattribute required="false" type="Boolean" default="false"
	 */
	public void setStatus(String status) {
		this.statusAttr = status;
	}
}
